#include "../inc/Ui/TrafficJamForm.h"
#include "../inc/Ui/WebForm.h"
#include"../inc/Utility/MyParserTiengViet.h"
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Base::Collection;
using namespace Osp::Media;
using namespace Osp::Graphics;

int TrafficJamForm::FormId = -1;

TrafficJamForm::TrafficJamForm(void) {
}

TrafficJamForm::~TrafficJamForm(void) {
}

bool TrafficJamForm::Initialize() {
	Form::Construct(L"TrafficJamForm");

	return true;
}

result TrafficJamForm::OnInitializing(void) {
	result r = E_SUCCESS;

	pLabelThongBao = static_cast<Label*> (GetControl("IDC_LABEL_THONGBAO"));
	// TODO: Add your initialization code here
	this->SetSoftkeyActionId(SOFTKEY_0, ID_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	pDiaDiem = static_cast<EditField*> (GetControl("IDC_EDITFIELD_DIADIEM"));

	return r;
}

result TrafficJamForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void TrafficJamForm::OnActionPerformed(const Osp::Ui::Control &source,
		int actionId) {
	FormManager *pFormManager = null;
	switch (actionId) {
	case ID_OK: {
		AppLog("Ok");
		if (pDiaDiem->GetTextLength() == 0) {
			MessageBox msg;
			msg.Construct("Chào bạn", "Bạn chưa nhập địa điểm",
					MSGBOX_STYLE_OK, 3000);
			int i = 0;
			msg.ShowAndWait(i);
			return;
		} else {
			timKetXe();
		}
	}
		break;
	case ID_BACK: {
		AppLog("Back");
		FormManager::GetInstance(pFormManager);
		pFormManager->SetCurrentForm(WebForm::GetFormId());
	}
		break;
	default:
		break;
	}
}
void TrafficJamForm::timKetXe() {
	String address,addressWeb;
	MyParserTiengViet *pTiengViet=new MyParserTiengViet();
	address=pTiengViet->TranformSignToUnsignInTiengViet(pDiaDiem->GetText());
	AppLog("%S", address.GetPointer());

	addressWeb.Format(500, L"http://toannhanb7.somee.com/Default.aspx?command=gettrafficjam&diaDiem=%S",address.GetPointer());
	//addressWeb.Format(500, L"http://localhost:49610/Default.aspx?diaDiem=%S",address.GetPointer());

	Downloader::downloadAndSave(addressWeb,L"/Home/DiaDiemKetXe.xml", this);
}

void TrafficJamForm::OnDownloadComplete(String url, String path) {
	AppLog("Download successful! \n");
	HienThiThongTinKetXe();
}
void TrafficJamForm::HienThiThongTinKetXe() {
	MyParserXml *myPa = new MyParserXml();
	LinkedListT<DiaDiemKetXe*>*list = new LinkedListT<DiaDiemKetXe*> ();
	list = myPa->getLayMangDiaDiemKetXe("/Home/DiaDiemKetXe.xml");

	if (list->GetCount() <= 0) {
		pLabelThongBao->SetText("Không có thông tin kẹt xe");
	} else {
		pLabelThongBao->SetText("");
		FormManager *pFormManager = null;
		FormManager::GetInstance(pFormManager);
		Form *pWebForm = null;
		result r = pFormManager->GetForm(WebForm::GetFormId(), pWebForm);
		if (IsFailed(r)) {
			return;
		}
		pFormManager->SetCurrentForm(WebForm::GetFormId());
		(static_cast<WebForm*> (pWebForm))->hienThiViTriKetXe(
				"/Home/DiaDiemKetXe.xml");
	}
	pLabelThongBao->RequestRedraw(true);
}
void TrafficJamForm::OnDownloadFailed(String url, String path) {
	pLabelThongBao->SetText("Việc lấy dữ liệu thất bại");
	pLabelThongBao->RequestRedraw(true);
}
